package com.lzh.controller;




import cn.hutool.core.bean.BeanUtil;
import com.lzh.bean.Customer;
import com.lzh.common.bean.PageBean;
import com.lzh.common.bean.ResultBean;
import com.lzh.common.controller.BaseController;
import com.lzh.common.group.Isave;
import com.lzh.common.group.V;
import com.lzh.common.utils.Assert;
import com.lzh.query.CustomerQuery;
import com.lzh.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@Api(tags = "客户管理")
public class CustomerController extends BaseController {

    @Autowired(required = false)
    private CustomerService customerService;

    /**
     * 保存用户
     * @return
     */
    @ApiOperation("保存客户")
    @PostMapping("/customer")
    public ResultBean save(@Validated({Isave.class}) @RequestBody Customer customer, BindingResult result) {
        V.validate(result); //进行数据验证的
        int save = customerService.save(customer);
        Assert.isZerp(save,"保存失败");
        return saveResult(save);
    }

    /**
     * 删除客户
     * @param id
     * @return
     */
    @ApiImplicitParam(name = "id", value = "客户id", dataTypeClass = Integer.class, paramType = "query", required = true)
    @ApiOperation("删除客户")
    @DeleteMapping("/customer")
    public ResultBean deleteCustomer(@RequestParam(required = true) int id) {
        int delete = customerService.delete(id);
        return deleteResult(delete);
    }

    /**
     * 更新用户
     * @param customer
     * @param result
     * @return
     */
    @ApiOperation("更新客户")
    @PutMapping("/customer")
    public ResultBean updateCustomer(@RequestBody Customer customer) {
        int update = customerService.update(customer);
        return updateResult(update);
    }

    /**
     * 查询客户列表
     */
    @ApiOperation("查询客户")
    @GetMapping("/customer")
    public ResultBean get(int id) {
        System.out.println("访问"+id);
        Customer customer = customerService.get(id);
        return success("客户信息", customer);
    }


    @ApiOperation("查询客户列表")
    @PostMapping("/customer/list")
    public ResultBean<PageBean<List<Customer>>> query(@RequestBody CustomerQuery query) {
//        PageBean bean = customerService.queryByMap(BeanUtil.beanToMap(query));
        PageBean bean = customerService.query(query);
        return success("客户列表", bean);
  }
}
